package com.igroupes.ovineherd.model.table;

import cn.hutool.core.convert.Convert;
import com.igroupes.ovineherd.entity.DataModelTableEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.Style.HiddenStyle;
import com.igroupes.ovineherd.model.Style.SwitchStyle;
import com.igroupes.ovineherd.model.Style.TextStyle;
import com.igroupes.ovineherd.model.Style.TextareaStyle;
import com.igroupes.ovineherd.model.enums.SqlFieldKey;
import com.igroupes.ovineherd.model.utils.DbName;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonTableQuery;
import com.igroupes.ovineherd.query.TableFieldQuery;
import com.igroupes.ovineherd.util.SpringPropertyUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Data
public class TableAttribute {

    static final String TABLEATTRIBUTE = "TABLEATTRIBUTE";
    private HiddenStyle id;
    private TextStyle name;
    private TextareaStyle desc;
    private SwitchStyle softDel;
    private SwitchStyle addDatetime;
    private SwitchStyle addOperator;
    private SwitchStyle editDatetime;
    private SwitchStyle editOperator;
    private String beanType; //所属包

    public TableAttribute() {
        this.name = new TextStyle("名称","",true,10,"用于区分数据模型的每一个属性，同一模型的名字不能重复");
        this.desc = new TextareaStyle("描述", "", false, 1000, "字段底部显示的描述信息");
//        this.softDel = new SwitchStyle("真删除", BooleanKey.TRUE.getCode(), false,"真删除不会保留数据，假删除不会展示数据保留数据");
//        this.addDatetime = new SwitchStyle("添加时间", BooleanKey.TRUE.getCode(), false,"记录第一次编辑时间");
//        this.addOperator = new SwitchStyle("添加者", BooleanKey.TRUE.getCode(), false,"记录第一次编辑者身份");
//        this.editDatetime = new SwitchStyle("编辑时间", BooleanKey.TRUE.getCode(), false,"记录最后一次编辑时间");
//        this.editOperator = new SwitchStyle("编辑者", BooleanKey.TRUE.getCode(), false,"记录最近一次编辑者身份");
        this.beanType = TABLEATTRIBUTE;
    }
    public static TableAttribute getAttribute(ModelDataJsonTableQuery modelDataJsonTableQuery) {
        TableAttribute tableAttribute = new TableAttribute();
        tableAttribute.setId(new HiddenStyle(modelDataJsonTableQuery.getId()));
        tableAttribute.setName(new TextStyle("名称",modelDataJsonTableQuery.getName(),true,10,"用于区分数据模型的每一个属性，同一模型的名字不能重复"));
        tableAttribute.setDesc(new TextareaStyle("描述", modelDataJsonTableQuery.getDesc(), false, 1000, "字段底部显示的描述信息"));
//        tableAttribute.setSoftDel(new SwitchStyle("真删除", modelDataJsonTableQuery.getSoftDel(), true,"真删除不会保留数据，假删除不会展示数据保留数据"));
//        tableAttribute.setAddDatetime(new SwitchStyle("添加时间", modelDataJsonTableQuery.getAddDatetime(), true,""));
//        tableAttribute.setAddOperator(new SwitchStyle("添加者", modelDataJsonTableQuery.getAddOperator(), true,""));
//        tableAttribute.setEditDatetime(new SwitchStyle("编辑时间", modelDataJsonTableQuery.getEditDatetime(), true,"记录最后一次编辑时间"));
//        tableAttribute.setEditOperator(new SwitchStyle("编辑者", modelDataJsonTableQuery.getEditOperator(), true,"记录最近一次编辑者身份"));
        return tableAttribute;
    }
    public static Boolean verifyField(TableAttribute addressModelData) {
        if (null == addressModelData) {
            throw new ModelException("table校验错误,传值不能为空");
        }
        new TextStyle().verify(addressModelData.getName());
        new TextareaStyle().verify(addressModelData.getDesc());
//        new SwitchStyle().verify(addressModelData.getSoftDel());
//        new SwitchStyle().verify(addressModelData.getAddDatetime());
//        new SwitchStyle().verify(addressModelData.getAddOperator());
//        new SwitchStyle().verify(addressModelData.getEditDatetime());
//        new SwitchStyle().verify(addressModelData.getEditOperator());
        return true;
    }

    /**
     * 获得数据库基础字段列表
     *
     * @return
     */
    public static List<FieldQuery> sqlField(TableAttribute tableAttribute) {
        List<FieldQuery> fieldQueries = new ArrayList<>();
//        if(Convert.toInt(tableAttribute.getSoftDel().getValue()).equals(BooleanKey.TRUE.getCode())){
            FieldQuery fieldQuery1 = new FieldQuery(SqlFieldKey.ISDEL.getKey(), SqlFieldKey.ISDEL.getType(), SqlFieldKey.ISDEL.getLength(), BooleanKey.TRUE.getCode());
            fieldQueries.add(fieldQuery1);
//        }
//        if(Convert.toInt(tableAttribute.getAddDatetime().getValue()).equals(BooleanKey.TRUE.getCode())){
            FieldQuery fieldQuery2 = new FieldQuery(SqlFieldKey.ADDTIME.getKey(), SqlFieldKey.ADDTIME.getType(), SqlFieldKey.ADDTIME.getLength(), BooleanKey.TRUE.getCode());
//            fieldQuery.setDataModelAttributeEntity(new DataModelAttributeEntity("新增时间", SqlFieldKey.ADDTIME.getKey()));
            fieldQueries.add(fieldQuery2);
//        }
//        if(Convert.toInt(tableAttribute.getAddOperator().getValue()).equals(BooleanKey.TRUE.getCode())){
            FieldQuery fieldQuery3 =new FieldQuery(SqlFieldKey.ADDUSER.getKey(), SqlFieldKey.ADDUSER.getType(), SqlFieldKey.ADDUSER.getLength(), BooleanKey.TRUE.getCode());
//            fieldQuery.setDataModelAttributeEntity(new DataModelAttributeEntity("新增者", SqlFieldKey.ADDUSER.getKey()));
            fieldQueries.add(fieldQuery3);
//        }
//        if(Convert.toInt(tableAttribute.getEditDatetime().getValue()).equals(BooleanKey.TRUE.getCode())){
            FieldQuery fieldQuery4 = new FieldQuery(SqlFieldKey.EDITTIME.getKey(), SqlFieldKey.EDITTIME.getType(), SqlFieldKey.EDITTIME.getLength(), BooleanKey.TRUE.getCode());
//            fieldQuery.setDataModelAttributeEntity(new DataModelAttributeEntity("编辑时间", SqlFieldKey.EDITTIME.getKey()));
            fieldQueries.add(fieldQuery4);
//        }
//        if(Convert.toInt(tableAttribute.getEditOperator().getValue()).equals(BooleanKey.TRUE.getCode())){
            FieldQuery fieldQuery5 = new FieldQuery(SqlFieldKey.EDITUSER.getKey(), SqlFieldKey.EDITUSER.getType(), SqlFieldKey.EDITUSER.getLength(), BooleanKey.TRUE.getCode());
//            fieldQuery.setDataModelAttributeEntity(new DataModelAttributeEntity("编辑者", SqlFieldKey.EDITUSER.getKey()));
            fieldQueries.add(fieldQuery5);
//        }
        return fieldQueries;
    }

    /**
     * 获取表
     *
     * @param tableAttribute
     * @return
     */
    public static TableFieldQuery sqlTable(TableAttribute tableAttribute, DataModelTableEntity dataModelTableEntityOld) {
        TableFieldQuery tableFieldQuery = new TableFieldQuery();
        DataModelTableEntity dataModelTableEntity = new DataModelTableEntity();
        if(dataModelTableEntityOld == null){
            String tableName = DbName.getDbName(Convert.toStr(tableAttribute.getName().getValue()));
            if(StringUtils.isBlank(tableName)){
                throw new ModelException("获取表名失败");
            }
            tableFieldQuery.setTableName(tableName);
            dataModelTableEntity.setDbName(SpringPropertyUtils.modelTable() + "."+tableName);
        }else{
            dataModelTableEntity.setId(dataModelTableEntityOld.getId());
            tableAttribute.setId(new HiddenStyle(dataModelTableEntityOld.getId()));
            dataModelTableEntity.setDbName(dataModelTableEntityOld.getDbName());
        }
        dataModelTableEntity.setName(Convert.toStr(tableAttribute.getName().getValue()));
//        dataModelTableEntity.setAddDatetime(Convert.toInt(tableAttribute.getAddDatetime().getValue()));
//        dataModelTableEntity.setAddOperator(Convert.toInt(tableAttribute.getAddOperator().getValue()));
//        dataModelTableEntity.setEditDatetime(Convert.toInt(tableAttribute.getEditDatetime().getValue()));
//        dataModelTableEntity.setEditOperator(Convert.toInt(tableAttribute.getEditOperator().getValue()));
//        dataModelTableEntity.setSoftDel(Convert.toInt(tableAttribute.getSoftDel().getValue()));
        dataModelTableEntity.setBeanType(tableAttribute.getBeanType());
        if (null != tableAttribute.getDesc().getValue()) {
            dataModelTableEntity.setDesc(Convert.toStr(tableAttribute.getDesc().getValue()));
            tableFieldQuery.setTableComment(Convert.toStr(tableAttribute.getDesc().getValue()));
        }
        tableFieldQuery.setDataModelTableEntity(dataModelTableEntity);
        tableFieldQuery.setFieldList(sqlField(tableAttribute));
        return tableFieldQuery;
    }


}